package com.haixiaoke.saas.enterprise.bill.controller;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.bill.domain.HouseBill;
import com.haixiaoke.saas.bill.service.IHouseBillService;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BillStateEnum;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.contract.domain.LesseeContract;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

import static com.haixiaoke.saas.common.tenant.TenantContext.getTenantId;

/**
 * 入住账单Controller
 *
 * @author qixi
 * @date 2024-05-30
 */
@RestController
@RequestMapping("/enterprise/bill/houseBill")
public class EnterpriseHouseBillController extends BaseController {
    @Autowired
    private IHouseBillService houseBillService;

    /**
     * 查询入住账单列表
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:list')")
    @TenantScope
    @GetMapping("/list")
    public TableDataInfo list(HouseBill houseBill) {
        startPage();
        houseBill.setTenantId(getTenantId());
        List<HouseBill> list = houseBillService.selectHouseBillList(houseBill);
        return getDataTable(list);
    }

    /**
     * 导出入住账单列表
     */
    // @PreAuthorize("@ss.hasPermi('bill:houseBill:export')")
    @Log(title = "入住账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseBill houseBill) {
        List<HouseBill> list = houseBillService.selectHouseBillList(houseBill);
        ExcelUtil<HouseBill> util = new ExcelUtil<HouseBill>(HouseBill.class);
        util.exportExcel(response, list, "入住账单数据");
    }

    /**
     * 获取入住账单详细信息
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:query')")
    @GetMapping(value = "/{billId}")
    @TenantScope
    public AjaxResult getInfo(@PathVariable("billId") String billId) {
        return success(houseBillService.selectHouseBillByBillId(billId));
    }

    /**
     * 新增入住账单
     */
    // @PreAuthorize("@ss.hasPermi('bill:houseBill:add')")
    @Log(title = "入住账单", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody @Valid HouseBill houseBill) {
        houseBill.setTenantId(getTenantId());
        houseBill.setCreateBy(getUsername());
        houseBill.setDeptId(getDeptId());
        return toAjax(houseBillService.insertHouseBill(houseBill));
    }

    /**
     * 修改入住账单
     */
    // @PreAuthorize("@ss.hasPermi('bill:houseBill:edit')")
    @Log(title = "入住账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseBill houseBill) {
        return toAjax(houseBillService.updateHouseBill(houseBill));
    }

    /**
     * 删除入住账单
     */
    // @PreAuthorize("@ss.hasPermi('bill:houseBill:remove')")
    @Log(title = "入住账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable String[] billIds) {
        return toAjax(houseBillService.deleteHouseBillByBillIds(billIds));
    }


    /**
     * 新增入住账单
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:create')")
    @Log(title = "入住账单", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    @TenantScope
    public AjaxResult create(@RequestBody @Valid HouseBill houseBill) {
        houseBill.setTenantId(getTenantId());
        houseBill.setCreateBy(getUsername());
        houseBill.setDeptId(getDeptId());
        return toAjax(houseBillService.createHouseBill(houseBill));
    }

    /**
     * 查询未生效账单列表
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:list:invalid')")
    @TenantScope
    @GetMapping("/list/invalid")
    public TableDataInfo invalidList(HouseBill houseBill) {
        startPage();
        houseBill.setTenantId(getTenantId());
        List<HouseBill> list = houseBillService.selectInvalidHouseBillList(houseBill);
        return getDataTable(list);
    }

    /**
     * 注销账单
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:logout')")
    @GetMapping(value = "/logout/{billId}")
    @TenantScope
    public AjaxResult logout(@PathVariable("billId") String billId) {
        HouseBill houseBill = houseBillService.selectHouseBillByBillId(billId);
        if (houseBill == null) {
            throw new ServiceException("账单不存在");
        }

        if (!(StrUtil.equals(houseBill.getPayState(), BillStateEnum.BILL_STATE_UNPAID.getValue())
                || StrUtil.equals(houseBill.getPayState(), BillStateEnum.BILL_STATE_OVERDUE.getValue()))) {
            throw new ServiceException("账单状态异常，请刷新后再试");
        }
        houseBill.setPayState(BillStateEnum.BILL_STATE_LOGOUT.getValue());
        houseBill.setUpdateTime(DateUtils.getNowDate());
        houseBill.setUpdateBy(getUsername());
        return toAjax(houseBillService.updateHouseBill(houseBill));
    }


    /**
     * 拆分账单
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:disassemble')")
    @Log(title = "入住账单", businessType = BusinessType.UPDATE)
    @PostMapping("/disassemble")
    @TenantScope
    public AjaxResult disassemble(@RequestBody HouseBill houseBill) {
        return toAjax(houseBillService.disassembleHouseBill(houseBill));
    }


    /**
     * 线下支付
     *
     * @param houseBill
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bill:houseBill:offlinePayment')")
    @PostMapping("/offlinePayment")
    @TenantScope
    public AjaxResult offlinePayment(@RequestBody HouseBill houseBill) {
        return toAjax(houseBillService.offlinePayment(houseBill));
    }


    /**
     * 计算账单列表
     */
    @PostMapping("/calculate")
    @TenantScope
    public AjaxResult calculate(@RequestBody LesseeContract lesseeContract) {
        return success(houseBillService.calculate(lesseeContract));
    }


}
